/**
 * 
 */
package com.wubo.sec.web.action;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.util.StringUtils;
import com.wubo.sec.bean.UserAuthRelate;
import com.wubo.sec.service.IUserAuthService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-8
 * @version 1.0.01
 */
@Controller
public class UserAuthMgtAction extends BaseSecAction {
	@Autowired
	private IUserAuthService userAuthService;
	
	private String userCode;
	
	private List<String> authCodes;
	
	private String authCode;

	public IUserAuthService getUserAuthService() {
		return userAuthService;
	}

	public void setUserAuthService(IUserAuthService userAuthService) {
		this.userAuthService = userAuthService;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}
	
	public List<String> getAuthCodes() {
		return authCodes;
	}

	public void setAuthCodes(List<String> authCodes) {
		this.authCodes = authCodes;
	}

	public String show(){
		try{
			authCodes = userAuthService.getAuthCodes(userCode);
			jsonSuccess(new JSONSerializer().serialize(authCodes));
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String relate(){
		logger.debug("relate auths to user .UserCode:"+userCode+", AuthCodes:"+authCode);
		try{
			String[] temp = null;
			if(! StringUtils.isEmpty(authCode)){
				temp = authCode.split(",");
				authCodes  = Arrays.asList(temp);
			}
			userAuthService.relate(new UserAuthRelate(userCode, temp));
			
			jsonSuccess(null);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
}
